const modifiersSet = ['ctrl', 'alt', 'shift', 'meta']

const modifiersMap = {
  ctrl: 17,
  alt: 18,
  shift: 16,
  meta: 91,
}

export function checkModifier(event, modifiers) {
  if (!modifiers || typeof modifiers.match !== 'function' || typeof modifiers.toLowerCase !== 'function') modifiers = ''
  modifiers = modifiers.toLowerCase()
  const keyCode = event.keyCode || event.which
  for (let modifier of modifiersSet) {
    if (modifiers.match(modifier) || keyCode === modifiersMap[modifier]) {
      if (!event[modifier + 'Key']) return false
    } else {
      if (event[modifier + 'Key']) return false
    }
  }
  return true
}

export function matchModifier(modifiers) {
  if (!modifiers || typeof modifiers.match !== 'function' || typeof modifiers.toLowerCase !== 'function') modifiers = ''
  modifiers = modifiers.toLowerCase()
  const modifierMatchedObject = { ctrl: false, alt: false, shift: false, meta: false }
  for (let modifier of modifiersSet) {
    if (modifiers.match(modifier)) modifierMatchedObject[modifier] = true
  }
  return modifierMatchedObject
}
